package com.harunasan.carrental.api.serve.controller;


import com.harunasan.carrental.api.serve.domain.entity.OmsLendCheck;
import com.harunasan.carrental.api.serve.domain.entity.Order;
import com.harunasan.carrental.api.serve.domain.post.OmsLendCheckPost;
import com.harunasan.carrental.api.serve.domain.post.OmsReturnCheckPost;
import com.harunasan.carrental.api.serve.domain.post.OrderPost;
import com.harunasan.carrental.api.serve.domain.vo.OrderDetailVo;
import com.harunasan.carrental.api.serve.service.IOrderService;
import com.harunasan.carrental.common.api.CommonPage;
import com.harunasan.carrental.common.api.CommonResult;
import com.harunasan.carrental.common.query.CommonQueryParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author 梁毅
 * @since 2022-03-07 23:00:58
 */
@RestController
@Api(tags = "订单管理")
@RequestMapping("/api/serve/order")
public class OrderController {
    @Autowired
    private IOrderService orderService;

    @PostMapping()
    @ApiOperation("生成订单")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> orderCreate(@RequestBody OrderPost orderPost) {
        return CommonResult.success(orderService.create(orderPost));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除订单信息")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> orderRemove(@ApiParam(name = "id", value = "订单标识", required = true)
                                             @PathVariable Long id) {
        return CommonResult.success(orderService.delete(id));
    }

    @PutMapping("/{id}")
    @ApiOperation("修改订单信息")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> orderUpdate(@ApiParam(name = "id", value = "订单标识", required = true)
                                             @PathVariable Long id, @RequestBody OrderPost order) {
        return CommonResult.success(orderService.update(id,order));
    }

    @PutMapping("/lend/{id}")
    @ApiOperation("出车！")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> orderLend(@ApiParam(name = "id", value = "订单标识", required = true)
                                             @PathVariable Long id, @RequestBody OmsLendCheckPost lendCheckPost) {
        return CommonResult.success(orderService.lendCheckOrder(id,lendCheckPost));
    }

    @PutMapping("/return/{id}")
    @ApiOperation("还车！")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> orderReturn(@ApiParam(name = "id", value = "订单标识", required = true)
                                             @PathVariable Long id,@RequestBody OmsReturnCheckPost omsReturnCheckPost) {
        return CommonResult.success(orderService.returnCheckOrder(id,omsReturnCheckPost));
    }

    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> orderCancel(@ApiParam(name = "id", value = "订单标识", required = true)
                                             @PathVariable Long id) {
        return CommonResult.success(orderService.cancel(id));
    }

    @GetMapping()
    @ApiOperation("获取订单列表")
    public CommonResult<List<Order>> orderList(CommonQueryParam commonQueryParam) {
        commonQueryParam.setSize((int) orderService.count());
        return CommonResult.success(orderService.list(commonQueryParam));
    }

    @GetMapping("/{id}")
    @ApiOperation("获取订单详情")
    public CommonResult<Order> orderObj(@PathVariable Long id) {
        return CommonResult.success(orderService.detail(id));
    }

    @GetMapping("/all-detail/{id}")
    @ApiOperation("获取全套订单信息详情")
    public CommonResult<OrderDetailVo> allOrderDetail(@PathVariable Long id) {
        return CommonResult.success(orderService.allDetail(id));
    }

    @GetMapping("/page")
    @ApiOperation("获取订单分页")
    public CommonResult<CommonPage<Order>> orderPage(CommonQueryParam commonQueryParam) {
        return CommonResult.success(CommonPage.restPage(orderService.pageList(commonQueryParam)));
    }

    @GetMapping("/page/by-store/{id}")
    @ApiOperation("根据门店id获取其订单分页")
    public CommonResult<CommonPage<Order>> orderPage(@PathVariable Long id,CommonQueryParam commonQueryParam) {
        return CommonResult.success(CommonPage.restPage(orderService.pageListByStoreId(id,commonQueryParam)));
    }
}
